<?php
namespace app\store\controller;

use think\Db;
use app\common\controller\Api;
use addons\shopro\model\Category as CategoryModel;

/****
 * Class    Store
 * @name    商品分类分类API文件
 * @author  晓月老板<1434926265@qq.com>
 * @date    2020/05/20 11:34 AM
 * @package app\api\controller
 */
class GoodsCategory extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    // 默认方法
    public function _initialize()
    {
        parent::_initialize();

        // 获取店铺ID
        $this->store_id = $this->request->request("store_id",0,"intval");
    }


    /***
     * @name    商户商品分类添加接口<按照后台权重排序>
     * @author  晓月老板<1434926265@qq.com>
     * @date    2020/07/06 14:03 PM
     * @return  string<json>
     */
    public function add()
    {

        if($this->store_id == 0){$this->error('参数有误!');}

        $insert_data = array();

        $insert_data['store_id'] = $this->store_id;
        $insert_data['name'] = $this->request->post('name');
        $insert_data['type'] = $this->request->post('type');
        $insert_data['image'] = $this->request->post('image');
        $insert_data['pid'] = $this->request->post('pid');
        $insert_data['description'] = $this->request->post('description');
        $insert_data['status'] = $this->request->post('status','normal');
        $insert_data['createtime'] = time();

        $result = CategoryModel::insert($insert_data);

        if($result){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }

    }


    /***
     * @name    商户商品分类编辑接口<按照后台权重排序>
     * @author  晓月老板<1434926265@qq.com>
     * @date    2020/07/06 14:03 PM
     * @return  string<json>
     */
    public function edit()
    {

        $category_id = $this->request->post('category_id',0,'intval');

        if($this->store_id == 0 || $category_id == 0){$this->error('参数有误!');}

        $update_data = array();

        $update_data['store_id'] = $this->store_id;
        $update_data['name'] = $this->request->post('name');
        $update_data['type'] = $this->request->post('type');
        $update_data['image'] = $this->request->post('image');
        $update_data['pid'] = $this->request->post('pid');
        $update_data['description'] = $this->request->post('description');
        $update_data['status'] = $this->request->post('status','normal');
        $update_data['updatetime'] = time();

        $result = CategoryModel::where(array('id'=>$category_id,'store_id'=>$this->store_id))->update($update_data);

        if(!$result){
            $this->error('编辑失败');
        }else{
            $this->success('编辑成功');
        }

    }


    /***
     * @name    商户商品分类编辑接口<按照后台权重排序>
     * @author  晓月老板<1434926265@qq.com>
     * @date    2020/07/06 14:03 PM
     * @return  string<json>
     */
    public function delete()
    {

        $category_id = $this->request->post('category_id',0,'intval');

        if($this->store_id == 0 || $category_id == 0){$this->error('参数有误!');}

        $result = CategoryModel::where(array('id'=>$category_id,'store_id'=>$this->store_id))->delete();

        if(!$result){
            $this->error('删除失败');
        }else{
            $this->success('删除成功');
        }

    }




}
